<!DOCTYPE html><html lang="zh-CN" data-theme="dark"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>docker学习 | Pei's Blog</title><meta name="keywords" content="docker"><meta name="author" content="Pei"><meta name="copyright" content="Pei"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#0d0d0d"><meta name="description" content="自己看b站尚硅谷docker 2022版教程所记笔记，纯手敲，持续记录中......">
<meta property="og:type" content="article">
<meta property="og:title" content="docker学习">
<meta property="og:url" content="https://blog.goku.top/posts/29609.html">
<meta property="og:site_name" content="Pei&#39;s Blog">
<meta property="og:description" content="自己看b站尚硅谷docker 2022版教程所记笔记，纯手敲，持续记录中......">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://qiniu.goku.top/blog/docker.jpeg">
<meta property="article:published_time" content="2022-03-08T13:09:45.000Z">
<meta property="article:modified_time" content="2023-10-17T00:12:33.721Z">
<meta property="article:author" content="Pei">
<meta property="article:tag" content="docker">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://qiniu.goku.top/blog/docker.jpeg"><link rel="shortcut icon" href="http://qiniu.goku.top/blog/icon.png"><link rel="canonical" href="https://blog.goku.top/posts/29609"><link rel="preconnect" href="//cdn.jsdelivr.net"/><meta name="google-site-verification" content="培的博客"/><meta name="baidu-site-verification" content="培的博客"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/font-awesome/6.4.2/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/snackbarjs/1.1.0/snackbar.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/fancyapps-ui/5.0.22/fancybox/fancybox.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"search.xml","languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: undefined,
  noticeOutdate: {"limitDay":365,"position":"top","messagePrev":"该文章已发布","messageNext":"天，文章信息部分有效性请以实际为准。"},
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":400},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: true,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: {"chs_to_cht":"你已切换为繁体","cht_to_chs":"你已切换为简体","day_to_night":"你已切换为深色模式","night_to_day":"你已切换为浅色模式","bgLight":"#49b1f5","bgDark":"#121212","position":"top-center"},
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: true,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'docker学习',
  isPost: true,
  isHome: false,
  isHighlightShrink: undefined,
  isToc: true,
  postUpdate: '2023-10-17 08:12:33'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="/html/css/plane_v2.css"><link rel="stylesheet" href="/html/css/universe.css"><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/4.1.1/animate.min.css" media="defer" onload="this.media='all'"><link rel="stylesheet" href="/html/css/fengche.css"><link rel="stylesheet" href="/html/css/twikoo_beautify.css"><link rel="stylesheet" type="text/css" href="/html/css/heoMainColor.css"><link rel="stylesheet" type="text/css" href="/html/css/poem.css"><link rel="stylesheet" type="text/css" href="/html/css/snackbar.css"><link rel="stylesheet" href="https://cdn1.tianli0.top/gh/zhheo/Post-Abstract-AI@0.15.2/tianli_gpt.css"><link rel="stylesheet" href="/html/css/goku.css"><link rel="stylesheet" href="/html/css/custom_foot.css"><meta name="generator" content="Hexo 6.3.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://thirdqq.qlogo.cn/g?b=sdk&amp;k=C88w6ZftQk9ibgdcM4sH4jg&amp;s=140&amp;t=1657271033" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data is-center"><div class="data-item"><a href="/archives/"><div class="headline">文章</div><div class="length-num">38</div></a></div><div class="data-item"><a href="/tags/"><div class="headline">标签</div><div class="length-num">23</div></a></div><div class="data-item"><a href="/categories/"><div class="headline">分类</div><div class="length-num">20</div></a></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/random.html"><i class="fa-fw fa-solid fa-shuffle"></i><span> 随机一文</span></a></div><div class="menus_item"><a class="site-page" href="/webstack.html"><i class="fa-fw fa-solid fa-star"></i><span> 网址导航</span></a></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fa fa-heartbeat"></i><span> 娱乐</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/posts/d10ad42e.html"><span> 堵猫猫</span></a></li><li><a class="site-page child" href="/posts/fc4d392a.html"><span> 烟花秀</span></a></li><li><a class="site-page child" href="/html/TheMatrix/index.html"><span> 黑客帝国</span></a></li><li><a class="site-page child" href="/html/Glare/index.html"><span> 炫光</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/artitalk"><i class="fa-fw fas fa-comment-dots"></i><span> 说说</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于我</span></a></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fa-solid fa-blog"></i><span> 博客</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></li></ul></div></div></div></div><div class="post" id="body-wrap"><header class="not-top-img" id="page-header"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">Pei's Blog</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/random.html"><i class="fa-fw fa-solid fa-shuffle"></i><span> 随机一文</span></a></div><div class="menus_item"><a class="site-page" href="/webstack.html"><i class="fa-fw fa-solid fa-star"></i><span> 网址导航</span></a></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fa fa-heartbeat"></i><span> 娱乐</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/posts/d10ad42e.html"><span> 堵猫猫</span></a></li><li><a class="site-page child" href="/posts/fc4d392a.html"><span> 烟花秀</span></a></li><li><a class="site-page child" href="/html/TheMatrix/index.html"><span> 黑客帝国</span></a></li><li><a class="site-page child" href="/html/Glare/index.html"><span> 炫光</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/artitalk"><i class="fa-fw fas fa-comment-dots"></i><span> 说说</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于我</span></a></div><div class="menus_item"><a class="site-page group hide" href="javascript:void(0);"><i class="fa-fw fa-solid fa-blog"></i><span> 博客</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></li><li><a class="site-page child" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></li></ul></div></div></div><div id="nav-right"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i></a></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav></header><main class="layout" id="content-inner"><div id="post"><div id="post-info"><h1 class="post-title">docker学习</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="fa-fw post-meta-icon far fa-calendar-alt"></i><span class="post-meta-label">发表于</span><time datetime="2022-03-08T13:09:45.000Z" title="发表于 2022-03-08 21:09:45">2022-03-08</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E5%AD%A6%E4%B9%A0/">学习</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">6.2k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>22分钟</span></span></div></div></div><article class="post-content" id="article-container"><div class="note blue icon flat"><i class="note-icon fas fa-bullhorn"></i><p>本人白天空闲时看b站尚硅谷docker 2022版教程所记的笔记，笔记内容来源于视频中老师的文档内容，晚上会抽空整理至此博客上。</p>
</div>

<h1 id="安装步骤"><a href="#安装步骤" class="headerlink" title="安装步骤"></a>安装步骤</h1><h2 id="卸载旧版本"><a href="#卸载旧版本" class="headerlink" title="卸载旧版本"></a>卸载旧版本</h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">yum remove docker \</span><br><span class="line">                  docker-client \</span><br><span class="line">                  docker-client-latest \</span><br><span class="line">                  docker-common \</span><br><span class="line">                  docker-latest \</span><br><span class="line">                  docker-latest-logrotate \</span><br><span class="line">                  docker-logrotate \</span><br><span class="line">                  docker-engine</span><br></pre></td></tr></table></figure>

<p>yum安装gcc相关</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">yum -y install gcc</span><br><span class="line">yum -y install gcc-c++</span><br></pre></td></tr></table></figure>

<h2 id="安装需要的软件包"><a href="#安装需要的软件包" class="headerlink" title="安装需要的软件包"></a>安装需要的软件包</h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yum install -y yum-utils</span><br></pre></td></tr></table></figure>

<h2 id="设置stable镜像仓库"><a href="#设置stable镜像仓库" class="headerlink" title="设置stable镜像仓库"></a>设置stable镜像仓库</h2><div class="note red flat"><p>注意：国内由于众所周知的原因，网速可能会特别慢，这个可以使用阿里云仓库替换</p>
</div>

<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo</span><br></pre></td></tr></table></figure>

<p>替换为</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo</span><br></pre></td></tr></table></figure>

<h2 id="更新yum软件包索引"><a href="#更新yum软件包索引" class="headerlink" title="更新yum软件包索引"></a>更新yum软件包索引</h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yum makecache fast</span><br></pre></td></tr></table></figure>

<h2 id="安装docker-ce"><a href="#安装docker-ce" class="headerlink" title="安装docker ce"></a>安装docker ce</h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yum install docker-ce docker-ce-cli containerd.io</span><br></pre></td></tr></table></figure>

<h2 id="启动docker"><a href="#启动docker" class="headerlink" title="启动docker"></a>启动docker</h2><p>启动docker服务</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">systemctl start docker</span><br></pre></td></tr></table></figure>

<p>查看是否成功运行</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ps -ef|grep docker</span><br></pre></td></tr></table></figure>

<h2 id="测试"><a href="#测试" class="headerlink" title="测试"></a>测试</h2><p>查看docker版本</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker version</span><br></pre></td></tr></table></figure>

<p>hello-world</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker run hello-world</span><br></pre></td></tr></table></figure>

<h2 id="卸载"><a href="#卸载" class="headerlink" title="卸载"></a>卸载</h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">systemctl stop docker</span><br><span class="line">yum remove docker-ce docker-ce-cli containerd.io </span><br><span class="line">rm -rf /var/lib/docker</span><br><span class="line">rm -rf /var/lib/docker</span><br></pre></td></tr></table></figure>

<h2 id="阿里云容器镜像加速"><a href="#阿里云容器镜像加速" class="headerlink" title="阿里云容器镜像加速"></a>阿里云容器镜像加速</h2><p><a target="_blank" rel="noopener" href="https://promotion.aliyun.com/ntms/act/kubernetes.html">https://promotion.aliyun.com/ntms/act/kubernetes.html</a><br>控制台（右上角）-产品与服务（左侧菜单）-弹性计算-容器镜像服务-镜像工具-镜像加速器<img src= "" data-lazy-src="https://ghproxy.com/https://raw.githubusercontent.com/sgr997/images/main/blog/%E9%98%BF%E9%87%8C%E4%BA%91docker%E9%95%9C%E5%83%8F%E5%8A%A0%E9%80%9F1.png" alt="阿里云docker镜像加速1"></p>
<p><img src= "" data-lazy-src="https://ghproxy.com/https://raw.githubusercontent.com/sgr997/images/main/blog/%E9%98%BF%E9%87%8C%E4%BA%91docker%E9%95%9C%E5%83%8F%E5%8A%A0%E9%80%9F2.png" alt="阿里云docker镜像加速2"></p>
<p><img src= "" data-lazy-src="https://ghproxy.com/https://raw.githubusercontent.com/sgr997/images/main/blog/%E9%98%BF%E9%87%8C%E4%BA%91docker%E9%95%9C%E5%83%8F%E5%8A%A0%E9%80%9F3.png" alt="阿里云docker镜像加速3"></p>
<p>如上图，弹性计算中的<strong>容器镜像服务</strong></p>
<p><img src= "" data-lazy-src="https://ghproxy.com/https://raw.githubusercontent.com/sgr997/images/main/blog/%E9%98%BF%E9%87%8C%E4%BA%91docker%E9%95%9C%E5%83%8F%E5%8A%A0%E9%80%9F4.png" alt="阿里云docker镜像加速4"></p>
<p>执行下方命令即可。</p>
<h2 id="永远的hello-world"><a href="#永远的hello-world" class="headerlink" title="永远的hello-world"></a>永远的hello-world</h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker run hello-world</span><br></pre></td></tr></table></figure>

<p>run都做了什么，看下图</p>
<p><img src= "" data-lazy-src="http://assets.processon.com/chart_image/6094b26d1e0853762876a50b.png" alt="run都做了什么"></p>
<h2 id="底层原理"><a href="#底层原理" class="headerlink" title="底层原理"></a>底层原理</h2><ol>
<li>docker有着比虚拟机更少的抽象层<br>由于docker不需要Hypervisor（虚拟机）实现硬件资源虚拟化，运行在docker容器上的程序直接使用的都是实际物理机的硬件资源。因此在CPU、内存利用率上docker将会在效率上有明显优势</li>
<li>docker利用的是宿主机的内核，而不需要加载操作系统OS内核<br>当新建一个容器时，docker不需要和虚拟机一样重新加载一个操作系统内核。进而避免引寻、加载操作系统内核返回等比较费时费资源的过程，当新建一个虚拟机时，虚拟机软件需要加载OS，返回新建过程是分钟级别的。而docker由于直接利用宿主机的操作系统，则省略了返回过程，因此新建一个docker容器只需要几秒钟。</li>
</ol>
<table>
<thead>
<tr>
<th></th>
<th>docker</th>
<th>虚拟机（VM）</th>
</tr>
</thead>
<tbody><tr>
<td>操作系统</td>
<td>与宿主机共享OS</td>
<td>宿主机OS上运行虚拟机OS</td>
</tr>
<tr>
<td>存储大小</td>
<td>镜像小、便于存储与传输</td>
<td>镜像庞大（vmdk、vdi等）</td>
</tr>
<tr>
<td>运行性能</td>
<td>几乎无额外性能损失</td>
<td>操作系统额外的CPU、内存消耗</td>
</tr>
<tr>
<td>移植性</td>
<td>轻便、灵活、适应于Linux</td>
<td>笨重，与虚拟化技术耦合度高</td>
</tr>
<tr>
<td>硬件亲和性</td>
<td>面向软件开发者</td>
<td>面向硬件运维者</td>
</tr>
<tr>
<td>部署速度</td>
<td>快速、秒级</td>
<td>较慢，10s以上</td>
</tr>
</tbody></table>
<h1 id="Docker常用命令"><a href="#Docker常用命令" class="headerlink" title="Docker常用命令"></a>Docker常用命令</h1><h2 id="帮助启动类命令"><a href="#帮助启动类命令" class="headerlink" title="帮助启动类命令"></a>帮助启动类命令</h2><p>启动docker：systemctl start docker<br>停止docker：systemctl stop docker<br>重启docker：systemctl restart docker<br>查看docker状态：systemctl status docker<br>开机启动：systemctl enable docker<br>查看docker概要信息：docker info<br>查看docker总体帮助文档：docker –help<br>查看docker命令帮助文档：docker 具体命令 –help</p>
<h2 id="镜像命令"><a href="#镜像命令" class="headerlink" title="镜像命令"></a>镜像命令</h2><h3 id="docker-images"><a href="#docker-images" class="headerlink" title="docker images"></a>docker images</h3><p>说明：列出本地主机上的镜像</p>
<p><img src= "" data-lazy-src="https://ghproxy.com/https://raw.githubusercontent.com/sgr997/images/main/docker/20220309211032.png" alt="图示"></p>
<p>各个字段说明：</p>
<table>
<thead>
<tr>
<th>REPOSITORY</th>
<th>TAG</th>
<th>IMAGE_ID</th>
<th>CREATED</th>
<th>SIZE</th>
</tr>
</thead>
<tbody><tr>
<td>镜像的仓库源</td>
<td>镜像的标签版本号</td>
<td>镜像ID</td>
<td>镜像创建时间</td>
<td>镜像大小</td>
</tr>
</tbody></table>
<blockquote>
<p>同一仓库源可以有多个TAG版本，代表这个仓库源的不同个版本，我们使用REPOSITORY:TAG来定义不同的镜像。如果你不指定一个镜像的版本标签，例如你只使用ubuntu，docker将默认使用ubuntu:latest镜像</p>
</blockquote>
<p>OPTIONS说明：<br>-a：列出本地所有的镜像（含历史镜像）<br>-q：只显示镜像ID<br>例：<br>docker images -a<br>docker images -q<br>docker images -aq</p>
<h3 id="docker-search-某个xxx镜像名字"><a href="#docker-search-某个xxx镜像名字" class="headerlink" title="docker search 某个xxx镜像名字"></a>docker search 某个xxx镜像名字</h3><p>例：docker search redis</p>
<p><img src= "" data-lazy-src="https://ghproxy.com/https://raw.githubusercontent.com/sgr997/images/main/docker/20220309211400.png" alt="图示"></p>
<p>各个字段说明：</p>
<table>
<thead>
<tr>
<th>NAME</th>
<th>DESCRIPTION</th>
<th>STARTS</th>
<th>OFFICIAL</th>
<th>AUTOMATED</th>
</tr>
</thead>
<tbody><tr>
<td>镜像名称</td>
<td>镜像说明</td>
<td>数量</td>
<td>是否是官方的</td>
<td>是否是自动构建的</td>
</tr>
</tbody></table>
<p>OPTIONS说明：<br>–limit N：只列出前N条<br>例：docker search –limit 5 redis</p>
<h3 id="docker-pull-某个xxx镜像名字"><a href="#docker-pull-某个xxx镜像名字" class="headerlink" title="docker pull 某个xxx镜像名字"></a>docker pull 某个xxx镜像名字</h3><h3 id="下载镜像"><a href="#下载镜像" class="headerlink" title="下载镜像"></a>下载镜像</h3><p>docker pull 镜像名字[:TAG]<br>docker pull 镜像名字：没有TAG就是最新版，等价于 docker pull 镜像名字[:latest]<br>例：<br>docker pull redis 等价于 docker pull redis:latest<br>docker pull redis:6.0.8</p>
<h3 id="docker-system-df-查看镜像-x2F-容器-x2F-数据卷所占的空间"><a href="#docker-system-df-查看镜像-x2F-容器-x2F-数据卷所占的空间" class="headerlink" title="docker system df: 查看镜像&#x2F;容器&#x2F;数据卷所占的空间"></a>docker system df: 查看镜像&#x2F;容器&#x2F;数据卷所占的空间</h3><h3 id="docker-rmi"><a href="#docker-rmi" class="headerlink" title="docker rmi"></a>docker rmi</h3><p>删除镜像<br>-f：强制删除<br>删除多个：docker rmi -f 镜像名字1:TAG 镜像名字2:TAG<br>删除单个：docker rmi -f 镜像id<br>删除全部(参数续传)：docker rmi -f $(docker images -qa)<br>例：<br>docker rmi -f redis<br>docker rmi -f feb5aw8ab7<br>docker rmi -f $(docker images -qa)</p>
<h3 id="面试题：谈谈docker虚悬镜像是什么？"><a href="#面试题：谈谈docker虚悬镜像是什么？" class="headerlink" title="面试题：谈谈docker虚悬镜像是什么？"></a>面试题：谈谈docker虚悬镜像是什么？</h3><p>仓库名、标签都是&lt;none&gt;的镜像，俗称虚悬镜像(dangling image)</p>
<p>查询所有虚悬镜像：docker image ls -f dangling&#x3D;true</p>
<p>删除虚悬镜像：docker image prune</p>
<h3 id="思考"><a href="#思考" class="headerlink" title="思考"></a>思考</h3><p>结合我们Git的学习，大家猜猜是否会有 docker commit&#x2F;docker push？</p>
<h2 id="容器命令"><a href="#容器命令" class="headerlink" title="容器命令"></a>容器命令</h2><h3 id="新建-启动命令"><a href="#新建-启动命令" class="headerlink" title="新建+启动命令"></a>新建+启动命令</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker run [OPTIONS] IMAGE [COMMAND] [ARG...]</span><br></pre></td></tr></table></figure>

<p>OPTIONS说明（常用）：有些是一个减号，有些是两个减号<br>–name&#x3D;容器新名字：为容器指定一个名称<br>-d：后台运行容器并返回容器ID，也即启动守护式容器（后台运行）<br>-i：以交互模式运行容器，通常与-t同时使用<br>-t：为容器重新分配一个伪输入终端，通常与-i同时使用<br>也即启动交互式容器（前台有伪终端&#x2F;命令窗口，等待交互）<br>例：<br>docker run -it ubuntu &#x2F;bin&#x2F;bash<br>docker run -it –name&#x3D;myu1 ubuntu &#x2F;bin&#x2F;bash<br>&#x2F;bin&#x2F;bash：放在镜像名后的是命令，这里我们希望有个交互式shell，因此使用的是&#x2F;bin&#x2F;bash，默认也是，要退出终端(容器的命令窗口)，直接输入exit</p>
<blockquote>
<p>个人理解：相当于使用docker创建了一个ubuntu的linux机容器，在容器中常用的linux命令也都可以用</p>
</blockquote>
<p>-P：随机端口映射，大写P<br>-p：指定端口映射，小写p</p>
<table>
<thead>
<tr>
<th>参数</th>
<th>说明</th>
</tr>
</thead>
<tbody><tr>
<td>-p hostPort:containerPort</td>
<td>端口映射-p 8080:80</td>
</tr>
<tr>
<td>-p ip:hostPort:containerPort</td>
<td>配置监听地址-p 10.0.0.100:8080:80</td>
</tr>
<tr>
<td>-p ip:containerPort</td>
<td>随机分配端口-p 10.0.0.100:80</td>
</tr>
<tr>
<td>-p hostPort:containerPort:udp</td>
<td>指定协议-p 8080:80:tcp</td>
</tr>
<tr>
<td>-p 81:80 -p 443:443</td>
<td>指定多个</td>
</tr>
</tbody></table>
<h3 id="列出当前所有正在运行的容器"><a href="#列出当前所有正在运行的容器" class="headerlink" title="列出当前所有正在运行的容器"></a>列出当前所有正在运行的容器</h3><p>docker ps [OPTIONS]<br>-a：列出当前所有正在运行的容器+历史上运行过的<br>-l：显示最近创建的容器<br>-n 数量N：显示最近N个创建的容器<br>-q：静默模式，只显示容器编号<br>STATUS：Up表示正在运行</p>
<h3 id="退出容器"><a href="#退出容器" class="headerlink" title="退出容器"></a>退出容器</h3><p>两种退出方式<br>exit：run进去容器，exit退出，容器停止<br>ctrl+p+q：run进去容器，ctrl+p+q退出，容器不停止</p>
<h3 id="启动已经停止运行的容器"><a href="#启动已经停止运行的容器" class="headerlink" title="启动已经停止运行的容器"></a>启动已经停止运行的容器</h3><p>docker start 容器ID或者容器名</p>
<h3 id="重启容器"><a href="#重启容器" class="headerlink" title="重启容器"></a>重启容器</h3><p>docker start 容器ID或者容器名</p>
<h3 id="停止容器"><a href="#停止容器" class="headerlink" title="停止容器"></a>停止容器</h3><p>docker restart 容器ID或者容器名</p>
<h3 id="强制停止容器"><a href="#强制停止容器" class="headerlink" title="强制停止容器"></a>强制停止容器</h3><p>docker kill 容器ID或者容器名</p>
<h3 id="删除已停止的容器"><a href="#删除已停止的容器" class="headerlink" title="删除已停止的容器"></a>删除已停止的容器</h3><p>docker rm 容器ID<br>强制删除：docker rm -f 容器ID<br>一次性删除多个容器实例：<br>docker rm -f $(docker ps -a -q)<br>docker ps -a -q|xargs docker rm</p>
<h3 id="以下是重要指令"><a href="#以下是重要指令" class="headerlink" title="以下是重要指令"></a>以下是重要指令</h3><h3 id="启动守护式容器（后台服务器）"><a href="#启动守护式容器（后台服务器）" class="headerlink" title="启动守护式容器（后台服务器）"></a>启动守护式容器（后台服务器）</h3><p>在大部分场景，我们希望docker的服务是后台运行的，我们可以通过-d指定容器的后台运行模式。</p>
<p>docker run -d 容器名</p>
<p>假设使用docker run -d centos启动了一个容器</p>
<p>问题：使用docker ps -a进行查看，<strong>会发现容器已经退出</strong></p>
<p>很重要的说明一点：<strong>Docker容器后台运行，就必须有一个前台进程</strong>。</p>
<p>容器运行的命令如果不是那些<strong>一直挂起的命令</strong>（top，tail），就是会自动退出的。</p>
<p>这个是docker的机制问题，比如你的web容器，我们以nginx为例，正常情况下，我们配置启动服务只需要启动响应的service即可。例如service nginx start，但是，这样做，nginx为后台进程模式运行，就导致docker前台没有运行的应用，这样的容器后台启动后，会立即自杀，因为他觉得没事可做了。</p>
<p>所以，最佳的解决是，<strong>将你要运行的程序以前台进程的形式运行，常见的就是命令行模式，表示我还有交互操作，别中断，O(∩_∩)O哈哈~</strong></p>
<ul>
<li>redis前后台启动演示case<ul>
<li>前台交互式启动<ul>
<li>docker run -it redis</li>
</ul>
</li>
<li>后台守护式启动<ul>
<li>docker run -d redis</li>
</ul>
</li>
</ul>
</li>
</ul>
<h3 id="查看容器日志"><a href="#查看容器日志" class="headerlink" title="查看容器日志"></a>查看容器日志</h3><p>docker logs 容器id</p>
<h3 id="查看容器内运行的进程"><a href="#查看容器内运行的进程" class="headerlink" title="查看容器内运行的进程"></a>查看容器内运行的进程</h3><p>docker top 容器id</p>
<h3 id="查看容器内部细节"><a href="#查看容器内部细节" class="headerlink" title="查看容器内部细节"></a>查看容器内部细节</h3><p>docker inspect 容器id</p>
<h3 id="进入正在运行的容器并以命令行交互"><a href="#进入正在运行的容器并以命令行交互" class="headerlink" title="进入正在运行的容器并以命令行交互"></a>进入正在运行的容器并以命令行交互</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker exec -it 容器id /bin/bash</span><br></pre></td></tr></table></figure>

<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker attach 容器id</span><br></pre></td></tr></table></figure>

<p>上述两个区别：</p>
<p>attach直接进入容器启动命令的终端，不会启动新的进程，用exit退出，会导致容器的停止。</p>
<p>exec是在容器打开新的终端，并且可以启动新的进程，用exit退出，不会导致容器的停止。</p>
<p>推荐使用docker exec命令，因为退出容器终端，不会导致容器的停止。</p>
<p>一般用-d后台启动的程序，再用exec进入对应容器实例。</p>
<h3 id="从容器内拷贝文件到主机上"><a href="#从容器内拷贝文件到主机上" class="headerlink" title="从容器内拷贝文件到主机上"></a>从容器内拷贝文件到主机上</h3><p>docker cp 容器id:容器内路径 主机路径</p>
<h3 id="导入和导出容器"><a href="#导入和导出容器" class="headerlink" title="导入和导出容器"></a>导入和导出容器</h3><p>docker export 容器id &gt; 路径(默认当前路径)</p>
<p>export：导出容器的内容留作为一个tar归档文件[对应import命令]</p>
<p>docker import - 镜像用户名&#x2F;镜像名:镜像版本号</p>
<p>import：从tar包中的内容创建一个新的文件系统再导入为镜像[对应export]</p>
<p>案例：</p>
<p>docker export 容器id  &gt; 文件名.tar</p>
<p>cat 文件名.tar|docker import - 镜像用户名&#x2F;镜像名:镜像版本号</p>
<h3 id="小总结"><a href="#小总结" class="headerlink" title="小总结"></a>小总结</h3><table>
<thead>
<tr>
<th align="center">命令</th>
<th align="left">英文说明</th>
<th align="left">中文说明</th>
</tr>
</thead>
<tbody><tr>
<td align="center">attach</td>
<td align="left">Attach to a running container</td>
<td align="left">当前shell下attach 连接指定运行镜像</td>
</tr>
<tr>
<td align="center">build</td>
<td align="left">Build an image from a Dockerfile</td>
<td align="left">通过Dockerfile定制镜像</td>
</tr>
<tr>
<td align="center">commit</td>
<td align="left">Create a new image from a container changes</td>
<td align="left">提交当前容器为新的镜像</td>
</tr>
<tr>
<td align="center">cp</td>
<td align="left">Copy files&#x2F;folders from the containers filesystem to the host path</td>
<td align="left">从容器中拷贝指定文件或者目录到宿主机中</td>
</tr>
<tr>
<td align="center">create</td>
<td align="left">Create a new container</td>
<td align="left">创建一个新的容器，同run，但不启动容器</td>
</tr>
<tr>
<td align="center">diff</td>
<td align="left">Inspect changes on a container’s filesystem</td>
<td align="left">查看docker容器变化</td>
</tr>
<tr>
<td align="center">events</td>
<td align="left">Get real time events from the server</td>
<td align="left">从docker服务获取容器实时事件</td>
</tr>
<tr>
<td align="center">exec</td>
<td align="left">Run a command in an existing container</td>
<td align="left">在已存在的容器上运行命令</td>
</tr>
<tr>
<td align="center">export</td>
<td align="left">Stream the contents of a container as a tar archive</td>
<td align="left">导出容器的内容流作为-一个 tar归档文件[对应import]</td>
</tr>
<tr>
<td align="center">history</td>
<td align="left">Show the history of an image</td>
<td align="left">展示一个镜像形成历史</td>
</tr>
<tr>
<td align="center">images</td>
<td align="left">List images</td>
<td align="left">列出系统当前镜像</td>
</tr>
<tr>
<td align="center">import</td>
<td align="left">Create a new filesystem image from the contents of a tarball</td>
<td align="left">从tar包中的内容创建一个 新的文件系统映像[对应export]</td>
</tr>
<tr>
<td align="center">info</td>
<td align="left">Display system-wide information</td>
<td align="left">显示系统相关信息</td>
</tr>
<tr>
<td align="center">inspect</td>
<td align="left">Return low-level information on a container</td>
<td align="left">查看容器详细信息</td>
</tr>
<tr>
<td align="center">kill</td>
<td align="left">Kill a running container</td>
<td align="left">kill指定docker容器</td>
</tr>
<tr>
<td align="center">load</td>
<td align="left">Load an image from a tar archive</td>
<td align="left">从一个tar包中加载一个镜像[对应save]</td>
</tr>
<tr>
<td align="center">login</td>
<td align="left">Registe or Login to the docker registry server</td>
<td align="left">注册或者登陆一个docker源服务器</td>
</tr>
<tr>
<td align="center">logout</td>
<td align="left">Log out from a Docker registry server</td>
<td align="left">从当前Docker registry退出</td>
</tr>
<tr>
<td align="center">logs</td>
<td align="left">Fetch the logs of a container</td>
<td align="left">输出当前容器日志信息</td>
</tr>
<tr>
<td align="center">port</td>
<td align="left">Lookup the public-facing port which is NAT-ed to PRIVATE_PORT</td>
<td align="left">查看映射端口对应的容器内部源端口</td>
</tr>
<tr>
<td align="center">pause</td>
<td align="left">Pause all processes within a container</td>
<td align="left">暂停容器</td>
</tr>
<tr>
<td align="center">ps</td>
<td align="left">List containers</td>
<td align="left">列出容器列表</td>
</tr>
<tr>
<td align="center">pull</td>
<td align="left">Pull an image or a repository from the docker registry server</td>
<td align="left">从docker镜像源服务器拉取指定镜像或者库镜像</td>
</tr>
<tr>
<td align="center">push</td>
<td align="left">Push an image or a repository to the docker registry server</td>
<td align="left">推送指定镜像或者库镜像至docker源服务器</td>
</tr>
<tr>
<td align="center">restart</td>
<td align="left">Restart a running container</td>
<td align="left">重启运行的容器</td>
</tr>
<tr>
<td align="center">rm</td>
<td align="left">Remove one or more containers</td>
<td align="left">移除一个或者多个容器</td>
</tr>
<tr>
<td align="center">rmi</td>
<td align="left">Remove one or more images</td>
<td align="left">移除一个或多个镜像[无容器使用该镜像才可删除，否则需删除相关容器才可继续或-f强制删除]</td>
</tr>
<tr>
<td align="center">run</td>
<td align="left">Run a command in a new container</td>
<td align="left">创建一个新的容器并运行一个命令</td>
</tr>
<tr>
<td align="center">save</td>
<td align="left">Save an image to a tar archive</td>
<td align="left">保存一个镜像为一个tar包[对应 load]</td>
</tr>
<tr>
<td align="center">search</td>
<td align="left">Search for an image on the Docker Hub</td>
<td align="left">在docker hub中搜索镜像</td>
</tr>
<tr>
<td align="center">start</td>
<td align="left">Start a stopped containers</td>
<td align="left">启动容器</td>
</tr>
<tr>
<td align="center">stop</td>
<td align="left">Stop a running containers</td>
<td align="left">停止容器</td>
</tr>
<tr>
<td align="center">tag</td>
<td align="left">Tag an image into a repository</td>
<td align="left">给源中镜像打标签</td>
</tr>
<tr>
<td align="center">top</td>
<td align="left">Lookup the running processes of a container</td>
<td align="left">查看容器中运行的进程信息</td>
</tr>
<tr>
<td align="center">unpause</td>
<td align="left">Unpause a paused container</td>
<td align="left">取消暂停容器</td>
</tr>
<tr>
<td align="center">version</td>
<td align="left">Show the docker version information</td>
<td align="left">查看docker 版本号</td>
</tr>
<tr>
<td align="center">wait</td>
<td align="left">Block until a container stops, then print its exit code</td>
<td align="left">截取容器停止时的退出状态值</td>
</tr>
</tbody></table>
<h1 id="Docker镜像"><a href="#Docker镜像" class="headerlink" title="Docker镜像"></a>Docker镜像</h1><h2 id="镜像"><a href="#镜像" class="headerlink" title="镜像"></a>镜像</h2><p>是一种轻量级、可执行的独立软件包，它包含运行某个软件所需的所有内容，我们把应用程序和配置依赖打包好形成一个可交付的运行环境(包括代码、运行时需要的库、环境变量和配置文件等)，这个打包好的运行环境就是image镜像文件。<br>只有通过这个镜像文件才能生成Docker容器实例(类似Java中new出来一个对象)。</p>
<h2 id="分层的镜像"><a href="#分层的镜像" class="headerlink" title="分层的镜像"></a>分层的镜像</h2><p>以我们的pull为例，在下载的过程中我们可以看到docker的镜像好像在一层一层的下载</p>
<p><img src= "" data-lazy-src="https://ghproxy.com/https://raw.githubusercontent.com/sgr997/images/main/docker/20220311133628.png"></p>
<h2 id="UnionFS（联合文件系统）"><a href="#UnionFS（联合文件系统）" class="headerlink" title="UnionFS（联合文件系统）"></a>UnionFS（联合文件系统）</h2><p>UnionFS（联合文件系统):Union文件系统（UnionFS)是一种分层、轻量级并且高性能的文件系统，它支持<strong>对文件系统的修改作为一次提交来一层层的叠加</strong>，同时可以将不同目录挂载到同一个虚拟文件系统下(unite several directories into a single virtual<br>filesystem)。Union文件系统是Docker镜像的基础。<strong>镜像可以通过分层来进行继承</strong>，基于基础镜像（没有父镜像），可以制作各种具体的应用镜像。</p>
<p>特性:一次同时加载多个文件系统，但从外面看起来，只能看到一个文件系统，联合加载会把各层文件系统叠加起来，这样最终的</p>
<h2 id="Docker镜像加载原理"><a href="#Docker镜像加载原理" class="headerlink" title="Docker镜像加载原理"></a>Docker镜像加载原理</h2><p>docker的镜像实际上由一层一层的文件系统组成，这种层级的文件系统<strong>UnionFS</strong>。<br>bootfs(boot file system)主要包含<strong>bootloader</strong>和<strong>kernel</strong>, bootloader主要是引导加载kernel,Linux刚启动时会加载bootfs文件系统，<strong>在Docker镜像的最底层是引导文件系统bootfs</strong>。这一层与我们典型的LinuxUnix系统是一样的，包含boot加载器和内核。当boot加载完成之后整个内核就都在内存中了，此时内存的使用权已由bootfs转交给内核，此时系统也会卸载bootfs。<br><strong>rootfs</strong> (root file system)，在boots之上.包含的就是典型Linux系统中的l&#x2F;dev, &#x2F;proc, &#x2F;bin,&#x2F;etc等等标准目录和文件。roots就是各种不同的操作系统发行版，比如Ubuntu，Centos等等。</p>
<p><strong>平时我们安装进虚拟机的centos都是好几个G，为什么docker这里才200m？？</strong></p>
<p>对于一个精简的OS，<strong>rootfs</strong>可以很小，只需要包括最基本的命令、工具和程序库就可以了，因为底层直接用Host的kernel，自己只需要提供 <strong>rootfs</strong>就行了。由此可见对于不同的linux发行版, <strong>bootfs</strong>基本是一致的, <strong>rootts</strong>会有差别,因此不同的发行版可以公用<strong>bootfs</strong> 。</p>
<h2 id="为什么Docker镜像采用这种分层结构？"><a href="#为什么Docker镜像采用这种分层结构？" class="headerlink" title="为什么Docker镜像采用这种分层结构？"></a>为什么Docker镜像采用这种分层结构？</h2><p>镜像分层最大的一个好处就是共享资源，方便复制迁移，就是为了<strong>复用</strong>。<br>比如说有多个镜像都从相同的base镜像构建而来，那么Docker Host只需在磁盘上保存一份base镜像;同时内存中也只需加载一份base镜像，就可以为所有容器服务了。而且镜像的每一层都可以被共享。</p>
<h2 id="重点理解"><a href="#重点理解" class="headerlink" title="重点理解"></a>重点理解</h2><p><strong>Docker镜像层都是只读的，容器层是可写的</strong>。<br>当容器启动时，一个新的可写层被加载到镜像的顶部。<br>这一层通常被称作“容器层”，“容器层”之下的都叫“镜像层”。</p>
<blockquote>
<p>当容器启动时，一个新的可写层被加载到镜像的顶部。这一层通常被称作“容器层”，“容器层”之下的都叫“镜像层”。<br>所有对容器的改动-无论添加、删除、还是修改文件都只会发生在容器层中。只有容器层是可写的，容器层下面的所有镜像层都是只读的。</p>
</blockquote>
<h2 id="Docker镜像commit操作案例"><a href="#Docker镜像commit操作案例" class="headerlink" title="Docker镜像commit操作案例"></a>Docker镜像commit操作案例</h2><p>docker commit提交容器副本使之成为一个新的镜像</p>
<p><strong>docker commit -m&#x3D;”提交的描述信息” -a&#x3D;”作者” 容器ID 要创建的目标镜像名:[标签名]</strong></p>
<p>演示ubuntu容器安装vim：</p>
<ol>
<li><p>从Hub上下载ubuntu镜像到本地并成功运行</p>
<p>docker pull ubuntu</p>
<p>docker run -it ubuntu &#x2F;bin&#x2F;bash</p>
</li>
</ol>
<blockquote>
<p>原始的默认ubuntu镜像是不带着vim命令的，执行vim会报错</p>
</blockquote>
<ol start="2">
<li><p>外网连通的情况下，安装vim</p>
<p>在<strong>ubuntu容器中</strong>执行以下命令</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">更新包管理工具</span></span><br><span class="line">apt-get update</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">然后安装我们需要的vim</span></span><br><span class="line">apt-get -y install vim</span><br></pre></td></tr></table></figure></li>
<li><p>安装完成后，commit我们自己的新镜像</p>
<p>在主机执行commit命令，注意，不是ubuntu容器中</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">docker commit -m=<span class="string">&quot;提交的描述信息&quot;</span> -a=<span class="string">&quot;作者&quot;</span> 容器ID 要创建的目标镜像名:[标签名]</span></span><br><span class="line">docker commit -m=&quot;add vim cmd&quot; -a=&quot;zxp&quot; 刚才的容器ID custom/ubuntu:1.0</span><br></pre></td></tr></table></figure></li>
<li><p>启动我们的新镜像并和原来的对比</p>
<p>docker images会发现新镜像id会比原镜像大小大，因为新增了vim指令</p>
<p>docker run -it 新镜像id&#x2F;镜像名 &#x2F;bin&#x2F;bash</p>
<p>发现新容器直接可以使用vim命令</p>
<p><img src= "" data-lazy-src="https://ghproxy.com/https://raw.githubusercontent.com/sgr997/images/main/docker/20220311142806.png" alt="大小对比"></p>
</li>
</ol>
<h2 id="小总结-1"><a href="#小总结-1" class="headerlink" title="小总结"></a>小总结</h2><p>Docker中的镜像分层，支持通过扩展现有镜像，创建新的镜像。类似Java继承于一个Base基础类，自己再按需扩展。新镜像是从base镜像一层一层叠加生成的。每安装一个软件，就在现有镜像的基础上增加一层。</p>
<p><img src= "" data-lazy-src="https://ghproxy.com/https://raw.githubusercontent.com/sgr997/images/main/docker/20220311143120.png"></p>
<h1 id="本地镜像发布到阿里云"><a href="#本地镜像发布到阿里云" class="headerlink" title="本地镜像发布到阿里云"></a>本地镜像发布到阿里云</h1><p><img src= "" data-lazy-src="https://ghproxy.com/https://raw.githubusercontent.com/sgr997/images/main/docker/20220311143557.png" alt="流程"></p>
<h2 id="创建阿里云镜像仓库"><a href="#创建阿里云镜像仓库" class="headerlink" title="创建阿里云镜像仓库"></a>创建阿里云镜像仓库</h2><p><img src= "" data-lazy-src="https://ghproxy.com/https://raw.githubusercontent.com/sgr997/images/main/docker/20220311145551.png"></p>
<p><img src= "" data-lazy-src="https://ghproxy.com/https://raw.githubusercontent.com/sgr997/images/main/docker/20220311145614.png"></p>
<h2 id="按照命令来完成推送和拉取"><a href="#按照命令来完成推送和拉取" class="headerlink" title="按照命令来完成推送和拉取"></a>按照命令来完成推送和拉取</h2><p>创建成功后会有个<strong>操作指南</strong>，按照上边的命令来操作，如下，是我的</p>
<p>登录</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker login --username=用户名 registry.cn-hangzhou.aliyuncs.com</span><br></pre></td></tr></table></figure>

<p>推送</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">docker tag [ImageId] registry.cn-hangzhou.aliyuncs.com/zxp_learn/learn:[镜像版本号]</span><br><span class="line">docker push registry.cn-hangzhou.aliyuncs.com/zxp_learn/learn:[镜像版本号]</span><br></pre></td></tr></table></figure>

<p>拉取</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">docker pull registry.cn-hangzhou.aliyuncs.com/zxp_learn/learn:[镜像版本号]</span><br></pre></td></tr></table></figure>

<h1 id="本地镜像发布到私有库"><a href="#本地镜像发布到私有库" class="headerlink" title="本地镜像发布到私有库"></a>本地镜像发布到私有库</h1><h2 id="下载镜像Docker-Registry"><a href="#下载镜像Docker-Registry" class="headerlink" title="下载镜像Docker Registry"></a>下载镜像Docker Registry</h2><p>docker pull registry</p>
<h2 id="运行私有库Registry，相当于本地有个私有Docker-Hub"><a href="#运行私有库Registry，相当于本地有个私有Docker-Hub" class="headerlink" title="运行私有库Registry，相当于本地有个私有Docker Hub"></a>运行私有库Registry，相当于本地有个私有Docker Hub</h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">$ </span><span class="language-bash">docker run -d -p5000:5000 -v /zxp/myregistry/:/tmp/registry --privileged=<span class="literal">true</span> registry</span></span><br></pre></td></tr></table></figure>

<blockquote>
<p>-v &#x2F;zxp&#x2F;myregistry&#x2F;:&#x2F;tmp&#x2F;registry –privileged&#x3D;true后边会说，容器卷</p>
</blockquote>
<p>默认情况，仓库被创建在容器的**&#x2F;var&#x2F;lib&#x2F;registry**目录下，建议自行用容器卷映射，方便与宿主机联调</p>
<h2 id="案例演示创建一个新镜像，ubuntu安装ifconfig命令"><a href="#案例演示创建一个新镜像，ubuntu安装ifconfig命令" class="headerlink" title="案例演示创建一个新镜像，ubuntu安装ifconfig命令"></a>案例演示创建一个新镜像，ubuntu安装ifconfig命令</h2><p>与<a href="#Docker%E9%95%9C%E5%83%8Fcommit%E6%93%8D%E4%BD%9C%E6%A1%88%E4%BE%8B">Docker镜像commit操作案例</a>一节一样，只不过命令为</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">apt-get update</span><br><span class="line">apt-get install net-tools</span><br></pre></td></tr></table></figure>

<h2 id="curl验证私服库上有什么镜像"><a href="#curl验证私服库上有什么镜像" class="headerlink" title="curl验证私服库上有什么镜像"></a>curl验证私服库上有什么镜像</h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">curl -XGET http://宿主机IP:5000/v2/_catalog</span><br></pre></td></tr></table></figure>

<p>返回{“repositories”:[]}说明什么也没有</p>
<blockquote>
<p>因为我使用的是腾讯云，如果使用公网IP访问，需要在网站控制台开放5000端口，阿里云应该也一样</p>
</blockquote>
<h2 id="将新镜像xxx修改符合私服规范的tag"><a href="#将新镜像xxx修改符合私服规范的tag" class="headerlink" title="将新镜像xxx修改符合私服规范的tag"></a>将新镜像xxx修改符合私服规范的tag</h2><p>按照公式：docker tag 镜像:Tag 私服Host:私服Port&#x2F;可以自定义的镜像名:可以自定义的Tag好</p>
<p>例如：docker tag custom&#x2F;ubuntu:1.1 127.0.0.1:5000&#x2F;custom&#x2F;ubuntu:1.1</p>
<blockquote>
<p>个人理解:</p>
<p>私服Host:私服Port这里就相当于告诉了docker我们的私服地址</p>
</blockquote>
<h2 id="修改配置文件使之支持http"><a href="#修改配置文件使之支持http" class="headerlink" title="修改配置文件使之支持http"></a>修改配置文件使之支持http</h2><p>cat &#x2F;etc&#x2F;docker&#x2F;daemon.json</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  &quot;registry-mirrors&quot;: [&quot;https://wxwkue7f.mirror.aliyuncs.com&quot;]</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>vim命令增加:”insecure-registries”:[“宿主机IP:私服端口”]，<strong>注意中间有个逗号</strong>，别漏了。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  &quot;registry-mirrors&quot;: [&quot;https://wxwkue7f.mirror.aliyuncs.com&quot;],</span><br><span class="line">  &quot;insecure-registries&quot;:[&quot;宿主机IP:私服端口&quot;]</span><br><span class="line">&#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>上述理由: docker默认不允许htp方式推送镜像，通过配置选项来取消这个限制。&#x3D;&#x3D;&#x3D;&#x3D;&gt;<strong>修改完后如果不生效，建议重启docker</strong>(systemctl restart docker)，重启之后记得run registry镜像，我就忘了，push的时候一直提示拒绝😂</p>
<h2 id="push推送到私服库"><a href="#push推送到私服库" class="headerlink" title="push推送到私服库"></a>push推送到私服库</h2><p>docker push 容器名:tag</p>
<p>例：</p>
<p>docker push 127.0.0.1:5000&#x2F;custom&#x2F;ubuntu:1.1</p>
<blockquote>
<p>个人理解：发现这里不用填写私服地址，因为在<a href="#%E5%B0%86%E6%96%B0%E9%95%9C%E5%83%8Fxxx%E4%BF%AE%E6%94%B9%E7%AC%A6%E5%90%88%E7%A7%81%E6%9C%8D%E8%A7%84%E8%8C%83%E7%9A%84tag">将新镜像xxx修改符合私服规范的tag</a>一节中已经按照规范告诉了docker我们要push至哪个私服</p>
</blockquote>
<h2 id="curl验证私服库上有什么镜像-1"><a href="#curl验证私服库上有什么镜像-1" class="headerlink" title="curl验证私服库上有什么镜像"></a>curl验证私服库上有什么镜像</h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">curl -XGET http://宿主机IP:5000/v2/_catalog</span><br></pre></td></tr></table></figure>

<p>可以看到新推送的：{“repositories”:[“custom&#x2F;ubuntu”]}</p>
<h2 id="pull到本地运行"><a href="#pull到本地运行" class="headerlink" title="pull到本地运行"></a>pull到本地运行</h2><p>docker pull 私服IP:私服端口&#x2F;镜像名:镜像Tag</p>
<p>例如：</p>
<p>docker pull 127.0.0.1:5000&#x2F;custom&#x2F;ubuntu:1.1</p>
<p>docker run -it 127.0.0.1:5000&#x2F;custom&#x2F;ubuntu:1.1</p>
<p>测试ifconfig即可</p>
<p>O(∩_∩)O~~~</p>
<h1 id="Docker容器卷"><a href="#Docker容器卷" class="headerlink" title="Docker容器卷"></a>Docker容器卷</h1><h2 id="坑，–privileged-x3D-true"><a href="#坑，–privileged-x3D-true" class="headerlink" title="坑，–privileged&#x3D;true"></a>坑，–privileged&#x3D;true</h2><p>Docker挂载主机目录访问如果出现<strong>cannot open directory .: Permission denied</strong></p>
<p>解决办法:在挂载目录后多加一个–privileged&#x3D;true参数即可。</p>
<p>如果是CentOS7安全模块会比之前系统版本加强，不安全的会先禁止，所以目录挂载的情况被默认为不安全的行为,<br>在SELinux里面挂载目录被禁止掉了，如果要开启，我们一般使用–privileged&#x3D;true命令，扩大容器的权限解决挂载目录没有权限的问题，也即使用该参数，container内的root拥有真正的root权限，否则，container内的root只是外部的一个普通用户权限。</p>
<h2 id="还记得上一讲中的参数v么"><a href="#还记得上一讲中的参数v么" class="headerlink" title="还记得上一讲中的参数v么"></a>还记得上一讲中的参数v么</h2><p>docker run -d -p5000:5000 -v &#x2F;zxp&#x2F;myregistry&#x2F;:&#x2F;tmp&#x2F;registry –privileged&#x3D;true registry</p>
<p>-v &#x2F;zxp&#x2F;myregistry&#x2F;:&#x2F;tmp&#x2F;registry</p>
<p>-v：要添加容器卷</p>
<p>冒号左侧（&#x2F;zxp&#x2F;myregistry&#x2F;）：宿主机的路径</p>
<p>冒号右侧（&#x2F;tmp&#x2F;registry）：容器内的路径</p>
<p>默认情况下，仓库被创建在容器的&#x2F;var&#x2F;lib&#x2F;registry目录下，建议自行用容器卷映射，方便与宿主机联调</p>
<h2 id="是什么"><a href="#是什么" class="headerlink" title="是什么"></a>是什么</h2><ul>
<li><p>一句话:有点类似我们Redis里面的rdb和aof文件</p>
</li>
<li><p>将docker容器内的数据保存进宿主机的磁盘中</p>
</li>
<li><p>运行一个带有容器卷存储功能的容器实例</p>
<p>docker run -it –privileged&#x3D;true -v &#x2F;宿主机绝对路径目录:&#x2F;容器内目录 镜像名</p>
</li>
</ul>
<h2 id="能干嘛"><a href="#能干嘛" class="headerlink" title="能干嘛"></a>能干嘛</h2><p>*将运用与运行的环境打包镜像，run后形成容器实例运行，但是我们对数据的要求希望是持久化的。<br>Docker容器产生的数据，如果不备份，那么当容器实例删除后，容器内的数据自然也就没有了。为了能保存数据在docker中我们使用卷。<br>特点：</p>
<ol>
<li>数据卷可在容器之间共享或重用数据</li>
<li>卷中的更改可以直接实时生效，爽</li>
<li>数据卷中的更改不会包含在镜像的更新中</li>
<li>数据卷的生命周期一直持续到没有容器使用它为止</li>
</ol>
<h2 id="数据卷案例"><a href="#数据卷案例" class="headerlink" title="数据卷案例"></a>数据卷案例</h2><h3 id="宿主vs容器之间映射添加容器卷"><a href="#宿主vs容器之间映射添加容器卷" class="headerlink" title="宿主vs容器之间映射添加容器卷"></a>宿主vs容器之间映射添加容器卷</h3><p>直接命令添加</p>
<ul>
<li><p>命令</p>
<p>docker run -it –privileged&#x3D;true -v &#x2F;宿主机绝对路径目录:&#x2F;容器内目录 镜像名</p>
<p>例：docker run -d -p5000:5000 -v &#x2F;zxp&#x2F;myregistry&#x2F;:&#x2F;tmp&#x2F;registry –privileged&#x3D;true registry</p>
</li>
<li><p>查看数据卷是否挂载成功</p>
<p>docker inspect 容器id</p>
<p>查看Mounts字段</p>
</li>
<li><p>容器和宿主机之间数据共享</p>
<ol>
<li>docker修改，主机同步获得</li>
<li>主机修改，docker同步获得</li>
<li>docker容器stop，主机修改，docker容器重启看数据是否同步</li>
</ol>
</li>
</ul>
<h3 id="读写规则映射添加说明"><a href="#读写规则映射添加说明" class="headerlink" title="读写规则映射添加说明"></a>读写规则映射添加说明</h3><ul>
<li><p>读写（默认）</p>
<p>docker run -it –privileged&#x3D;true -v &#x2F;宿主机绝对路径目录:&#x2F;容器内目录:rw   镜像名</p>
<p>例：docker run -d -p5000:5000 –privileged&#x3D;true -v &#x2F;zxp&#x2F;myregistry&#x2F;:&#x2F;tmp&#x2F;registry:rw  registry</p>
</li>
<li><p>只读（ro）</p>
<p>容器实例内部被限制，只能读取不能写</p>
<p>docker run -it –privileged&#x3D;true -v &#x2F;宿主机绝对路径目录:&#x2F;容器内目录:ro   镜像名</p>
<p>例：docker run -d -p5000:5000 –privileged&#x3D;true -v &#x2F;zxp&#x2F;myregistry&#x2F;:&#x2F;tmp&#x2F;registry:ro  registry</p>
<blockquote>
<p>ro是read only的缩写</p>
</blockquote>
</li>
</ul>
<h3 id="卷的继承和共享"><a href="#卷的继承和共享" class="headerlink" title="卷的继承和共享"></a>卷的继承和共享</h3><ol>
<li><p>容器1完成和宿主机的映射</p>
</li>
<li><p>容器2继承容器1的规则</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">父类可是是容器名和容器<span class="built_in">id</span></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">例:docker run -it --volumes-from 0a5dcb032464 --name=u3 ubuntu</span></span><br><span class="line">docker run -it --privileged=true --volumes-from 父类  镜像名</span><br></pre></td></tr></table></figure></li>
</ol>
</article><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/docker/">docker</a></div><div class="post_share"><div class="social-share" data-image="http://qiniu.goku.top/blog/docker.jpeg" data-sites="wechat,weibo,qq,facebook,twitter"></div><link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/social-share.js/1.0.16/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.bootcdn.net/ajax/libs/social-share.js/1.0.16/js/social-share.min.js" defer></script></div></div><div class="post-reward"><div class="reward-button"><i class="fas fa-qrcode"></i> 打赏</div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="/img/wxpay.jpeg" target="_blank"><img class="post-qr-code-img" src="/img/wxpay.jpeg" alt="微信"/></a><div class="post-qr-code-desc">微信</div></li><li class="reward-item"><a href="/img/alipay.jpeg" target="_blank"><img class="post-qr-code-img" src="/img/alipay.jpeg" alt="支付宝"/></a><div class="post-qr-code-desc">支付宝</div></li></ul></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/posts/56715.html"><img class="prev-cover" src="http://qiniu.goku.top/blog/ms_kj.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">（转载）设计模式二三事</div></div></a></div><div class="next-post pull-right"><a href="/posts/43570.html"><img class="next-cover" src="http://qiniu.goku.top/blog/pandas.jpeg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">Pandas入门学习</div></div></a></div></nav><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> 评论</span></div></div><div class="comment-wrap"><div><div id="twikoo-wrap"></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://thirdqq.qlogo.cn/g?b=sdk&amp;k=C88w6ZftQk9ibgdcM4sH4jg&amp;s=140&amp;t=1657271033" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">Pei</div><div class="author-info__description">躺平虽然不好，但真的很爽</div></div><div class="card-info-data is-center"><div class="card-info-data-item"><a href="/archives/"><div class="headline">文章</div><div class="length-num">38</div></a></div><div class="card-info-data-item"><a href="/tags/"><div class="headline">标签</div><div class="length-num">23</div></a></div><div class="card-info-data-item"><a href="/categories/"><div class="headline">分类</div><div class="length-num">20</div></a></div></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/sgr997"><i class="fab fa-github"></i><span>联系我</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/sgr997" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="http://www.coolapk.com/u/725902" target="_blank" title="酷安"><i class="fa-solid fa-c"></i></a><a class="social-icon" href="https://space.bilibili.com/49499302" target="_blank" title="bilibili"><i class="fa-brands fa-bilibili"></i></a></div></div><script src="https://npm.elemecdn.com/ethan4116-blog/lib/js/other/two-people/twopeople1.js"></script><script src="https://npm.elemecdn.com/ethan4116-blog/lib/js/other/two-people/zdog.dist.js"></script><script id="rendered-js" src="https://npm.elemecdn.com/ethan4116-blog/lib/js/other/two-people/twopeople.js"></script><style>.card-widget.card-announcement {
margin: 0;
align-items: center;
justify-content: center;
text-align: center;
}
canvas {
display: block;
margin: 0 auto;
cursor: move;
}</style><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%AE%89%E8%A3%85%E6%AD%A5%E9%AA%A4"><span class="toc-number">1.</span> <span class="toc-text">安装步骤</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8D%B8%E8%BD%BD%E6%97%A7%E7%89%88%E6%9C%AC"><span class="toc-number">1.1.</span> <span class="toc-text">卸载旧版本</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AE%89%E8%A3%85%E9%9C%80%E8%A6%81%E7%9A%84%E8%BD%AF%E4%BB%B6%E5%8C%85"><span class="toc-number">1.2.</span> <span class="toc-text">安装需要的软件包</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%AE%BE%E7%BD%AEstable%E9%95%9C%E5%83%8F%E4%BB%93%E5%BA%93"><span class="toc-number">1.3.</span> <span class="toc-text">设置stable镜像仓库</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%9B%B4%E6%96%B0yum%E8%BD%AF%E4%BB%B6%E5%8C%85%E7%B4%A2%E5%BC%95"><span class="toc-number">1.4.</span> <span class="toc-text">更新yum软件包索引</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AE%89%E8%A3%85docker-ce"><span class="toc-number">1.5.</span> <span class="toc-text">安装docker ce</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%90%AF%E5%8A%A8docker"><span class="toc-number">1.6.</span> <span class="toc-text">启动docker</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%B5%8B%E8%AF%95"><span class="toc-number">1.7.</span> <span class="toc-text">测试</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8D%B8%E8%BD%BD"><span class="toc-number">1.8.</span> <span class="toc-text">卸载</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%98%BF%E9%87%8C%E4%BA%91%E5%AE%B9%E5%99%A8%E9%95%9C%E5%83%8F%E5%8A%A0%E9%80%9F"><span class="toc-number">1.9.</span> <span class="toc-text">阿里云容器镜像加速</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%B0%B8%E8%BF%9C%E7%9A%84hello-world"><span class="toc-number">1.10.</span> <span class="toc-text">永远的hello-world</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%BA%95%E5%B1%82%E5%8E%9F%E7%90%86"><span class="toc-number">1.11.</span> <span class="toc-text">底层原理</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Docker%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4"><span class="toc-number">2.</span> <span class="toc-text">Docker常用命令</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%B8%AE%E5%8A%A9%E5%90%AF%E5%8A%A8%E7%B1%BB%E5%91%BD%E4%BB%A4"><span class="toc-number">2.1.</span> <span class="toc-text">帮助启动类命令</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%95%9C%E5%83%8F%E5%91%BD%E4%BB%A4"><span class="toc-number">2.2.</span> <span class="toc-text">镜像命令</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#docker-images"><span class="toc-number">2.2.1.</span> <span class="toc-text">docker images</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#docker-search-%E6%9F%90%E4%B8%AAxxx%E9%95%9C%E5%83%8F%E5%90%8D%E5%AD%97"><span class="toc-number">2.2.2.</span> <span class="toc-text">docker search 某个xxx镜像名字</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#docker-pull-%E6%9F%90%E4%B8%AAxxx%E9%95%9C%E5%83%8F%E5%90%8D%E5%AD%97"><span class="toc-number">2.2.3.</span> <span class="toc-text">docker pull 某个xxx镜像名字</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%8B%E8%BD%BD%E9%95%9C%E5%83%8F"><span class="toc-number">2.2.4.</span> <span class="toc-text">下载镜像</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#docker-system-df-%E6%9F%A5%E7%9C%8B%E9%95%9C%E5%83%8F-x2F-%E5%AE%B9%E5%99%A8-x2F-%E6%95%B0%E6%8D%AE%E5%8D%B7%E6%89%80%E5%8D%A0%E7%9A%84%E7%A9%BA%E9%97%B4"><span class="toc-number">2.2.5.</span> <span class="toc-text">docker system df: 查看镜像&#x2F;容器&#x2F;数据卷所占的空间</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#docker-rmi"><span class="toc-number">2.2.6.</span> <span class="toc-text">docker rmi</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%9D%A2%E8%AF%95%E9%A2%98%EF%BC%9A%E8%B0%88%E8%B0%88docker%E8%99%9A%E6%82%AC%E9%95%9C%E5%83%8F%E6%98%AF%E4%BB%80%E4%B9%88%EF%BC%9F"><span class="toc-number">2.2.7.</span> <span class="toc-text">面试题：谈谈docker虚悬镜像是什么？</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%80%9D%E8%80%83"><span class="toc-number">2.2.8.</span> <span class="toc-text">思考</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AE%B9%E5%99%A8%E5%91%BD%E4%BB%A4"><span class="toc-number">2.3.</span> <span class="toc-text">容器命令</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%96%B0%E5%BB%BA-%E5%90%AF%E5%8A%A8%E5%91%BD%E4%BB%A4"><span class="toc-number">2.3.1.</span> <span class="toc-text">新建+启动命令</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%97%E5%87%BA%E5%BD%93%E5%89%8D%E6%89%80%E6%9C%89%E6%AD%A3%E5%9C%A8%E8%BF%90%E8%A1%8C%E7%9A%84%E5%AE%B9%E5%99%A8"><span class="toc-number">2.3.2.</span> <span class="toc-text">列出当前所有正在运行的容器</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%80%80%E5%87%BA%E5%AE%B9%E5%99%A8"><span class="toc-number">2.3.3.</span> <span class="toc-text">退出容器</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%90%AF%E5%8A%A8%E5%B7%B2%E7%BB%8F%E5%81%9C%E6%AD%A2%E8%BF%90%E8%A1%8C%E7%9A%84%E5%AE%B9%E5%99%A8"><span class="toc-number">2.3.4.</span> <span class="toc-text">启动已经停止运行的容器</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%87%8D%E5%90%AF%E5%AE%B9%E5%99%A8"><span class="toc-number">2.3.5.</span> <span class="toc-text">重启容器</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%81%9C%E6%AD%A2%E5%AE%B9%E5%99%A8"><span class="toc-number">2.3.6.</span> <span class="toc-text">停止容器</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%BC%BA%E5%88%B6%E5%81%9C%E6%AD%A2%E5%AE%B9%E5%99%A8"><span class="toc-number">2.3.7.</span> <span class="toc-text">强制停止容器</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%A0%E9%99%A4%E5%B7%B2%E5%81%9C%E6%AD%A2%E7%9A%84%E5%AE%B9%E5%99%A8"><span class="toc-number">2.3.8.</span> <span class="toc-text">删除已停止的容器</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BB%A5%E4%B8%8B%E6%98%AF%E9%87%8D%E8%A6%81%E6%8C%87%E4%BB%A4"><span class="toc-number">2.3.9.</span> <span class="toc-text">以下是重要指令</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%90%AF%E5%8A%A8%E5%AE%88%E6%8A%A4%E5%BC%8F%E5%AE%B9%E5%99%A8%EF%BC%88%E5%90%8E%E5%8F%B0%E6%9C%8D%E5%8A%A1%E5%99%A8%EF%BC%89"><span class="toc-number">2.3.10.</span> <span class="toc-text">启动守护式容器（后台服务器）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%9F%A5%E7%9C%8B%E5%AE%B9%E5%99%A8%E6%97%A5%E5%BF%97"><span class="toc-number">2.3.11.</span> <span class="toc-text">查看容器日志</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%9F%A5%E7%9C%8B%E5%AE%B9%E5%99%A8%E5%86%85%E8%BF%90%E8%A1%8C%E7%9A%84%E8%BF%9B%E7%A8%8B"><span class="toc-number">2.3.12.</span> <span class="toc-text">查看容器内运行的进程</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%9F%A5%E7%9C%8B%E5%AE%B9%E5%99%A8%E5%86%85%E9%83%A8%E7%BB%86%E8%8A%82"><span class="toc-number">2.3.13.</span> <span class="toc-text">查看容器内部细节</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%BF%9B%E5%85%A5%E6%AD%A3%E5%9C%A8%E8%BF%90%E8%A1%8C%E7%9A%84%E5%AE%B9%E5%99%A8%E5%B9%B6%E4%BB%A5%E5%91%BD%E4%BB%A4%E8%A1%8C%E4%BA%A4%E4%BA%92"><span class="toc-number">2.3.14.</span> <span class="toc-text">进入正在运行的容器并以命令行交互</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BB%8E%E5%AE%B9%E5%99%A8%E5%86%85%E6%8B%B7%E8%B4%9D%E6%96%87%E4%BB%B6%E5%88%B0%E4%B8%BB%E6%9C%BA%E4%B8%8A"><span class="toc-number">2.3.15.</span> <span class="toc-text">从容器内拷贝文件到主机上</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AF%BC%E5%85%A5%E5%92%8C%E5%AF%BC%E5%87%BA%E5%AE%B9%E5%99%A8"><span class="toc-number">2.3.16.</span> <span class="toc-text">导入和导出容器</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B0%8F%E6%80%BB%E7%BB%93"><span class="toc-number">2.3.17.</span> <span class="toc-text">小总结</span></a></li></ol></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Docker%E9%95%9C%E5%83%8F"><span class="toc-number">3.</span> <span class="toc-text">Docker镜像</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%95%9C%E5%83%8F"><span class="toc-number">3.1.</span> <span class="toc-text">镜像</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%88%86%E5%B1%82%E7%9A%84%E9%95%9C%E5%83%8F"><span class="toc-number">3.2.</span> <span class="toc-text">分层的镜像</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#UnionFS%EF%BC%88%E8%81%94%E5%90%88%E6%96%87%E4%BB%B6%E7%B3%BB%E7%BB%9F%EF%BC%89"><span class="toc-number">3.3.</span> <span class="toc-text">UnionFS（联合文件系统）</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Docker%E9%95%9C%E5%83%8F%E5%8A%A0%E8%BD%BD%E5%8E%9F%E7%90%86"><span class="toc-number">3.4.</span> <span class="toc-text">Docker镜像加载原理</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%BA%E4%BB%80%E4%B9%88Docker%E9%95%9C%E5%83%8F%E9%87%87%E7%94%A8%E8%BF%99%E7%A7%8D%E5%88%86%E5%B1%82%E7%BB%93%E6%9E%84%EF%BC%9F"><span class="toc-number">3.5.</span> <span class="toc-text">为什么Docker镜像采用这种分层结构？</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%87%8D%E7%82%B9%E7%90%86%E8%A7%A3"><span class="toc-number">3.6.</span> <span class="toc-text">重点理解</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Docker%E9%95%9C%E5%83%8Fcommit%E6%93%8D%E4%BD%9C%E6%A1%88%E4%BE%8B"><span class="toc-number">3.7.</span> <span class="toc-text">Docker镜像commit操作案例</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%B0%8F%E6%80%BB%E7%BB%93-1"><span class="toc-number">3.8.</span> <span class="toc-text">小总结</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E6%9C%AC%E5%9C%B0%E9%95%9C%E5%83%8F%E5%8F%91%E5%B8%83%E5%88%B0%E9%98%BF%E9%87%8C%E4%BA%91"><span class="toc-number">4.</span> <span class="toc-text">本地镜像发布到阿里云</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%88%9B%E5%BB%BA%E9%98%BF%E9%87%8C%E4%BA%91%E9%95%9C%E5%83%8F%E4%BB%93%E5%BA%93"><span class="toc-number">4.1.</span> <span class="toc-text">创建阿里云镜像仓库</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%8C%89%E7%85%A7%E5%91%BD%E4%BB%A4%E6%9D%A5%E5%AE%8C%E6%88%90%E6%8E%A8%E9%80%81%E5%92%8C%E6%8B%89%E5%8F%96"><span class="toc-number">4.2.</span> <span class="toc-text">按照命令来完成推送和拉取</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E6%9C%AC%E5%9C%B0%E9%95%9C%E5%83%8F%E5%8F%91%E5%B8%83%E5%88%B0%E7%A7%81%E6%9C%89%E5%BA%93"><span class="toc-number">5.</span> <span class="toc-text">本地镜像发布到私有库</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%8B%E8%BD%BD%E9%95%9C%E5%83%8FDocker-Registry"><span class="toc-number">5.1.</span> <span class="toc-text">下载镜像Docker Registry</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%BF%90%E8%A1%8C%E7%A7%81%E6%9C%89%E5%BA%93Registry%EF%BC%8C%E7%9B%B8%E5%BD%93%E4%BA%8E%E6%9C%AC%E5%9C%B0%E6%9C%89%E4%B8%AA%E7%A7%81%E6%9C%89Docker-Hub"><span class="toc-number">5.2.</span> <span class="toc-text">运行私有库Registry，相当于本地有个私有Docker Hub</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%A1%88%E4%BE%8B%E6%BC%94%E7%A4%BA%E5%88%9B%E5%BB%BA%E4%B8%80%E4%B8%AA%E6%96%B0%E9%95%9C%E5%83%8F%EF%BC%8Cubuntu%E5%AE%89%E8%A3%85ifconfig%E5%91%BD%E4%BB%A4"><span class="toc-number">5.3.</span> <span class="toc-text">案例演示创建一个新镜像，ubuntu安装ifconfig命令</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#curl%E9%AA%8C%E8%AF%81%E7%A7%81%E6%9C%8D%E5%BA%93%E4%B8%8A%E6%9C%89%E4%BB%80%E4%B9%88%E9%95%9C%E5%83%8F"><span class="toc-number">5.4.</span> <span class="toc-text">curl验证私服库上有什么镜像</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%B0%86%E6%96%B0%E9%95%9C%E5%83%8Fxxx%E4%BF%AE%E6%94%B9%E7%AC%A6%E5%90%88%E7%A7%81%E6%9C%8D%E8%A7%84%E8%8C%83%E7%9A%84tag"><span class="toc-number">5.5.</span> <span class="toc-text">将新镜像xxx修改符合私服规范的tag</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BF%AE%E6%94%B9%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E4%BD%BF%E4%B9%8B%E6%94%AF%E6%8C%81http"><span class="toc-number">5.6.</span> <span class="toc-text">修改配置文件使之支持http</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#push%E6%8E%A8%E9%80%81%E5%88%B0%E7%A7%81%E6%9C%8D%E5%BA%93"><span class="toc-number">5.7.</span> <span class="toc-text">push推送到私服库</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#curl%E9%AA%8C%E8%AF%81%E7%A7%81%E6%9C%8D%E5%BA%93%E4%B8%8A%E6%9C%89%E4%BB%80%E4%B9%88%E9%95%9C%E5%83%8F-1"><span class="toc-number">5.8.</span> <span class="toc-text">curl验证私服库上有什么镜像</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#pull%E5%88%B0%E6%9C%AC%E5%9C%B0%E8%BF%90%E8%A1%8C"><span class="toc-number">5.9.</span> <span class="toc-text">pull到本地运行</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Docker%E5%AE%B9%E5%99%A8%E5%8D%B7"><span class="toc-number">6.</span> <span class="toc-text">Docker容器卷</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9D%91%EF%BC%8C%E2%80%93privileged-x3D-true"><span class="toc-number">6.1.</span> <span class="toc-text">坑，–privileged&#x3D;true</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%BF%98%E8%AE%B0%E5%BE%97%E4%B8%8A%E4%B8%80%E8%AE%B2%E4%B8%AD%E7%9A%84%E5%8F%82%E6%95%B0v%E4%B9%88"><span class="toc-number">6.2.</span> <span class="toc-text">还记得上一讲中的参数v么</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%98%AF%E4%BB%80%E4%B9%88"><span class="toc-number">6.3.</span> <span class="toc-text">是什么</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%83%BD%E5%B9%B2%E5%98%9B"><span class="toc-number">6.4.</span> <span class="toc-text">能干嘛</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%95%B0%E6%8D%AE%E5%8D%B7%E6%A1%88%E4%BE%8B"><span class="toc-number">6.5.</span> <span class="toc-text">数据卷案例</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%BF%E4%B8%BBvs%E5%AE%B9%E5%99%A8%E4%B9%8B%E9%97%B4%E6%98%A0%E5%B0%84%E6%B7%BB%E5%8A%A0%E5%AE%B9%E5%99%A8%E5%8D%B7"><span class="toc-number">6.5.1.</span> <span class="toc-text">宿主vs容器之间映射添加容器卷</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%AF%BB%E5%86%99%E8%A7%84%E5%88%99%E6%98%A0%E5%B0%84%E6%B7%BB%E5%8A%A0%E8%AF%B4%E6%98%8E"><span class="toc-number">6.5.2.</span> <span class="toc-text">读写规则映射添加说明</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%8D%B7%E7%9A%84%E7%BB%A7%E6%89%BF%E5%92%8C%E5%85%B1%E4%BA%AB"><span class="toc-number">6.5.3.</span> <span class="toc-text">卷的继承和共享</span></a></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/posts/31488961.html" title="解决macos安装软件后提示已损坏问题">解决macos安装软件后提示已损坏问题</a><time datetime="2023-10-10T05:22:13.909Z" title="发表于 2023-10-10 13:22:13">2023-10-10</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/posts/77213149.html" title="李跳跳自定义规则">李跳跳自定义规则</a><time datetime="2023-09-16T05:02:41.000Z" title="发表于 2023-09-16 13:02:41">2023-09-16</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/posts/AI%E8%81%8A%E5%A4%A9%E7%BD%91%E7%AB%99.html" title="AI聊天网站推荐">AI聊天网站推荐</a><time datetime="2023-06-19T11:39:41.000Z" title="发表于 2023-06-19 19:39:41">2023-06-19</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/posts/dd74890d.html" title="随记账本项目">随记账本项目</a><time datetime="2023-04-27T02:40:25.000Z" title="发表于 2023-04-27 10:40:25">2023-04-27</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/posts/fc4d392a.html" title="一场烟花送给你">一场烟花送给你</a><time datetime="2023-02-18T13:34:14.000Z" title="发表于 2023-02-18 21:34:14">2023-02-18</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div id="ft"><div class="ft-item-1"><div class="t-top"><div class="t-t-l"><p class="ft-t t-l-t">关于博客</p><div class="bg-ad"><div>衷心感谢每一位光临我的博客的朋友</div><div class="btn-xz-box"><a class="btn-xz" target="_blank" rel="noopener" href="https://gg.goku.top">聊天问答</a></div></div></div><div class="t-t-r"><p class="ft-t t-l-t">推荐网址</p><ul class="ft-links"><li><a target="_blank" rel="noopener" href="https://github.com/sgr997">Github</a><a href="/webstack.html">网址导航</a></li><li><a target="_blank" rel="noopener" href="http://qiniu.goku.top/wxpay.jpeg">来杯咖啡</a><a href="/artitalk/">留点什么</a></li><li><a href="/about/">关于博主</a><a href="/archives/">文章归档</a></li><li><a href="/categories/">文章分类</a><a href="/tags/">文章标签</a></li></ul></div></div></div><div class="ft-item-2"><p class="ft-t">推荐友链</p><div class="ft-img-group"><div class="img-group-item"><a href="/webstack.html"><img src="http://qiniu.goku.top/blog/icon.png" alt="网址导航"/></a></div></div></div></div><div class="copyright">&copy;2020 - 2023  <i id="heartbeat" class="fa fas fa-heartbeat"></i> Pei</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text"><a href="https://beian.miit.gov.cn/" target="_blank">豫ICP备2023023872号</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">本地搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="https://cdn.bootcdn.net/ajax/libs/fancyapps-ui/5.0.22/fancybox/fancybox.umd.js"></script><script src="https://cdn.bootcdn.net/ajax/libs/vanilla-lazyload/17.8.4/lazyload.iife.min.js"></script><script src="https://cdn.bootcdn.net/ajax/libs/snackbarjs/1.1.0/snackbar.min.js"></script><script src="/js/search/local-search.js"></script><script>var preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',preloader.endLoading())</script><div class="js-pjax"><script>(() => {
  const $mermaidWrap = document.querySelectorAll('#article-container .mermaid-wrap')
  if ($mermaidWrap.length) {
    window.runMermaid = () => {
      window.loadMermaid = true
      const theme = document.documentElement.getAttribute('data-theme') === 'dark' ? '' : ''

      Array.from($mermaidWrap).forEach((item, index) => {
        const mermaidSrc = item.firstElementChild
        const mermaidThemeConfig = '%%{init:{ \'theme\':\'' + theme + '\'}}%%\n'
        const mermaidID = 'mermaid-' + index
        const mermaidDefinition = mermaidThemeConfig + mermaidSrc.textContent
        mermaid.mermaidAPI.render(mermaidID, mermaidDefinition, (svgCode) => {
          mermaidSrc.insertAdjacentHTML('afterend', svgCode)
        })
      })
    }

    const loadMermaid = () => {
      window.loadMermaid ? runMermaid() : getScript('https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js').then(runMermaid)
    }

    window.pjax ? loadMermaid() : document.addEventListener('DOMContentLoaded', loadMermaid)
  }
})()</script><script>(()=>{
  const init = () => {
    twikoo.init(Object.assign({
      el: '#twikoo-wrap',
      envId: 'https://twikoo.goku.top/',
      region: '',
      onCommentLoaded: function () {
        btf.loadLightbox(document.querySelectorAll('#twikoo .tk-content img:not(.vemoji)'))
      }
    }, null))
  }

  const getCount = () => {
    twikoo.getCommentsCount({
      envId: 'https://twikoo.goku.top/',
      region: '',
      urls: [window.location.pathname],
      includeReply: false
    }).then(function (res) {
      document.getElementById('twikoo-count').innerText = res[0].count
    }).catch(function (err) {
      console.error(err);
    });
  }

  const runFn = () => {
    init()
    
  }

  const loadTwikoo = () => {
    if (typeof twikoo === 'object') {
      setTimeout(runFn,0)
      return
    } 
    getScript('https://cdn.bootcdn.net/ajax/libs/twikoo/1.6.20/twikoo.all.min.js').then(runFn)
  }

  if ('Twikoo' === 'Twikoo' || !true) {
    if (true) btf.loadComment(document.getElementById('twikoo-wrap'), loadTwikoo)
    else loadTwikoo()
  } else {
    window.loadOtherComment = () => {
      loadTwikoo()
    }
  }
})()</script></div><script>window.addEventListener('load', () => {
  const changeContent = (content) => {
    if (content === '') return content

    content = content.replace(/<img.*?src="(.*?)"?[^\>]+>/ig, '[图片]') // replace image link
    content = content.replace(/<a[^>]+?href=["']?([^"']+)["']?[^>]*>([^<]+)<\/a>/gi, '[链接]') // replace url
    content = content.replace(/<pre><code>.*?<\/pre>/gi, '[代码]') // replace code
    content = content.replace(/<[^>]+>/g,"") // remove html tag

    if (content.length > 150) {
      content = content.substring(0,150) + '...'
    }
    return content
  }

  const getComment = () => {
    const runTwikoo = () => {
      twikoo.getRecentComments({
        envId: 'https://twikoo.goku.top/',
        region: '',
        pageSize: 6,
        includeReply: true
      }).then(function (res) {
        const twikooArray = res.map(e => {
          return {
            'content': changeContent(e.comment),
            'avatar': e.avatar,
            'nick': e.nick,
            'url': e.url + '#' + e.id,
            'date': new Date(e.created).toISOString()
          }
        })

        saveToLocal.set('twikoo-newest-comments', JSON.stringify(twikooArray), 10/(60*24))
        generateHtml(twikooArray)
      }).catch(function (err) {
        const $dom = document.querySelector('#card-newest-comments .aside-list')
        $dom.innerHTML= "无法获取评论，请确认相关配置是否正确"
      })
    }

    if (typeof twikoo === 'object') {
      runTwikoo()
    } else {
      getScript('https://cdn.bootcdn.net/ajax/libs/twikoo/1.6.20/twikoo.all.min.js').then(runTwikoo)
    }
  }

  const generateHtml = array => {
    let result = ''

    if (array.length) {
      for (let i = 0; i < array.length; i++) {
        result += '<div class=\'aside-list-item\'>'

        if (false) {
          const name = 'data-lazy-src'
          result += `<a href='${array[i].url}' class='thumbnail'><img ${name}='${array[i].avatar}' alt='${array[i].nick}'></a>`
        }
        
        result += `<div class='content'>
        <a class='comment' href='${array[i].url}' title='${array[i].content}'>${array[i].content}</a>
        <div class='name'><span>${array[i].nick} / </span><time datetime="${array[i].date}">${btf.diffDate(array[i].date, true)}</time></div>
        </div></div>`
      }
    } else {
      result += '没有评论'
    }

    let $dom = document.querySelector('#card-newest-comments .aside-list')
    $dom.innerHTML= result
    window.lazyLoadInstance && window.lazyLoadInstance.update()
    window.pjax && window.pjax.refresh($dom)
  }

  const newestCommentInit = () => {
    if (document.querySelector('#card-newest-comments .aside-list')) {
      const data = saveToLocal.get('twikoo-newest-comments')
      if (data) {
        generateHtml(JSON.parse(data))
      } else {
        getComment()
      }
    }
  }

  newestCommentInit()
  document.addEventListener('pjax:complete', newestCommentInit)
})</script><canvas id="universe"></canvas><script defer src="/html/js/universe.js"></script><script defer src="/html/js/disablef12.js"></script><script src="/html/js/lunar.js"></script><script src="/html/js/day.js"></script><script type="text/javascript" src="https://cdn1.tianli0.top/npm/sweetalert2@8.19.0/dist/sweetalert2.all.js"></script><script async src="/html/js/justlovesmile.js"></script><script>let tianliGPT_postSelector = '\#post \#article-container';let tianliGPT_postURL = '\*/posts/\*';let tianliGPT_key = '123';</script><script data-pjax src="/html/js/summ.min.js"></script><script defer="defer" id="fluttering_ribbon" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-fluttering-ribbon.min.js"></script><script src="https://cdn.bootcdn.net/ajax/libs/pjax/0.2.8/pjax.min.js"></script><script>let pjaxSelectors = ["title","#config-diff","#body-wrap","#rightside-config-hide","#rightside-config-show",".js-pjax"]

var pjax = new Pjax({
  elements: 'a:not([target="_blank"])',
  selectors: pjaxSelectors,
  cacheBust: false,
  analytics: false,
  scrollRestoration: false
})

document.addEventListener('pjax:send', function () {

  // removeEventListener scroll 
  window.tocScrollFn && window.removeEventListener('scroll', window.tocScrollFn)
  window.scrollCollect && window.removeEventListener('scroll', scrollCollect)

  typeof preloader === 'object' && preloader.initLoading()
  document.getElementById('rightside').style.cssText = "opacity: ''; transform: ''"
  
  if (window.aplayers) {
    for (let i = 0; i < window.aplayers.length; i++) {
      if (!window.aplayers[i].options.fixed) {
        window.aplayers[i].destroy()
      }
    }
  }

  typeof typed === 'object' && typed.destroy()

  //reset readmode
  const $bodyClassList = document.body.classList
  $bodyClassList.contains('read-mode') && $bodyClassList.remove('read-mode')

})

document.addEventListener('pjax:complete', function () {
  window.refreshFn()

  document.querySelectorAll('script[data-pjax]').forEach(item => {
    const newScript = document.createElement('script')
    const content = item.text || item.textContent || item.innerHTML || ""
    Array.from(item.attributes).forEach(attr => newScript.setAttribute(attr.name, attr.value))
    newScript.appendChild(document.createTextNode(content))
    item.parentNode.replaceChild(newScript, item)
  })

  GLOBAL_CONFIG.islazyload && window.lazyLoadInstance.update()

  typeof chatBtnFn === 'function' && chatBtnFn()
  typeof panguInit === 'function' && panguInit()

  // google analytics
  typeof gtag === 'function' && gtag('config', '', {'page_path': window.location.pathname});

  // baidu analytics
  typeof _hmt === 'object' && _hmt.push(['_trackPageview',window.location.pathname]);

  typeof loadMeting === 'function' && document.getElementsByClassName('aplayer').length && loadMeting()

  // prismjs
  typeof Prism === 'object' && Prism.highlightAll()

  typeof preloader === 'object' && preloader.endLoading()
})

document.addEventListener('pjax:error', (e) => {
  if (e.request.status === 404) {
    pjax.loadUrl('/404.html')
  }
})</script></div></body></html>